package com.sun.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.enums.HttpEnum;
import com.sun.mapper.PurchaseOrdersMapper;
import com.sun.pojo.entity.PurchaseOrders;
import com.sun.pojo.vo.PurchaseOrdersTotalAmountVO;
import com.sun.pojo.vo.PurchaseOrdersVO;
import com.sun.pojo.vo.R;
import com.sun.service.PurchaseOrdersService;
import com.sun.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 采购订单表 记录采购订单信息(PurchaseOrders)表服务实现类
 *
 * @author makejava
 * @since 2024-10-25 08:18:16
 */
@Service("purchaseOrdersService")
public class PurchaseOrdersServiceImpl implements PurchaseOrdersService {
    @Autowired
    PurchaseOrdersMapper purchaseOrdersMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param orderId 主键
     * @return 实例对象
     */
    @Override
    public PurchaseOrders queryById(String orderId) {
        return purchaseOrdersMapper.queryById(orderId);
    }

    /**
     * 分页查询
     *
     * @param purchaseOrders 筛选条件
     * @param pageNum      分页对象
     * @return 查询结果
     */
    @Override
    public PageInfo<PurchaseOrdersVO> queryByPage(PurchaseOrders purchaseOrders, Integer pageNum) {
        PageHelper.startPage(pageNum,5);
        List<PurchaseOrdersVO> list = purchaseOrdersMapper.queryAllByLimit2(purchaseOrders);
        PageInfo<PurchaseOrdersVO> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 新增数据
     *
     * @param purchaseOrders 实例对象
     * @return 实例对象
     */
    @Override
    public R insert(PurchaseOrders purchaseOrders) {
        if (purchaseOrdersMapper.insert(purchaseOrders)>0){
            return R.success("添加成功");
        }
        return R.fail(HttpEnum.WARN.getCode(),"添加失败");
    }

    /**
     * 修改数据
     *
     * @param purchaseOrders 实例对象
     * @return 实例对象
     */
    @Override
    public R update(PurchaseOrders purchaseOrders) {
        if (purchaseOrdersMapper.update(purchaseOrders)>0){
            return R.success("修改成功");
        }
        return R.fail(HttpEnum.WARN.getCode(),"修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param orderId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String orderId) {
        if (purchaseOrdersMapper.deleteById(orderId)>0){
            return 1;
        }
        return 0;
    }

    @Override
    public R deliver(String orderId,String updateBy) {
        if (purchaseOrdersMapper.deliver(orderId,updateBy)>0){
            return R.success("发货成功");
        }else {
            return R.fail(HttpEnum.WARN.getCode(),"发货失败");
        }
    }

    @Override
    public R totalAmount(String updatedAt) {
        List<PurchaseOrdersTotalAmountVO> pavo = purchaseOrdersMapper.queryTotalAmount(updatedAt);
        int orderCount = 0;
        double totalAmount = 0;

        for (PurchaseOrdersTotalAmountVO order : pavo) {
            orderCount++;
            totalAmount += order.getTotalAmount();
        }
        return R.success("orderCount:"+orderCount+";totalAmount:"+totalAmount);
    }

    @Override
    public R totalAmountWeek() {
        // 使用java获取本周日期并且获取每天的订单量和交易额
        String[] week = DateUtil.getWeekDates();
        StringBuffer weekOrderCount = new StringBuffer();
        StringBuffer weekTotalAmount =  new StringBuffer();
        weekOrderCount.append("weekOrderCount:[");
        weekTotalAmount.append("weekTotalAmount:[");
        for (int i = 0 ; i < week.length ; i ++) {
            List<PurchaseOrdersTotalAmountVO> pavo = purchaseOrdersMapper.queryTotalAmount(week[i]);
            int orderCount = 0;
            double totalAmount = 0;

            for (PurchaseOrdersTotalAmountVO order : pavo) {
                orderCount++;
                totalAmount += order.getTotalAmount();
            }
            weekOrderCount.append(orderCount+",");
            weekTotalAmount.append(totalAmount+",");
        }
        weekOrderCount.append("]");
        weekTotalAmount.append("]");
        weekOrderCount.append(";"+weekTotalAmount);
        return R.success(weekOrderCount);
    }

    @Override
    public R totalAmountMonth() {
        // 使用java获取本周日期并且获取每天的订单量和交易额
        String[] month = DateUtil.getThisYearMonths();
        StringBuffer monthOrderCount = new StringBuffer();
        StringBuffer monthTotalAmount =  new StringBuffer();
        monthOrderCount.append("weekOrderCount:[");
        monthTotalAmount.append("weekTotalAmount:[");
        for (int i = 0 ; i < month.length ; i ++) {
            List<PurchaseOrdersTotalAmountVO> pavo = purchaseOrdersMapper.queryTotalAmount(month[i]);
            int orderCount = 0;
            double totalAmount = 0;

            for (PurchaseOrdersTotalAmountVO order : pavo) {
                orderCount++;
                totalAmount += order.getTotalAmount();
            }
            monthOrderCount.append(orderCount+",");
            monthTotalAmount.append(totalAmount+",");
        }
        monthOrderCount.append("]");
        monthTotalAmount.append("]");
        monthOrderCount.append(";"+monthTotalAmount);
        return R.success(monthOrderCount);
    }

    @Override
    public R queryWaitingAmount() {
        List<PurchaseOrdersTotalAmountVO> pavo = purchaseOrdersMapper.queryWaitingAmount();
        int orderCount = 0;
        double totalAmount = 0;

        for (PurchaseOrdersTotalAmountVO order : pavo) {
            orderCount++;
            totalAmount += order.getTotalAmount();
        }
        return R.success("orderCount:"+orderCount+";totalAmount:"+totalAmount);
    }
}
